{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、create  list列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "# 1.创建人脸列表\n",
    "faceListId = \"list_004\" # 设置人脸列表ID\n",
    "create_facelists_url =\"https://api-wal.cognitiveservices.azure.com/face/v1.0/facelists/{}\" # URL（见API文档）\n",
    "subscription_key =\"8fb912f9a500433bafb982f17772dc32\" # 密钥\n",
    "# 请求头\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 相关信息及识别模型的选用\n",
    "data = {\n",
    "  \"name\": \"sample_list\",\n",
    "  \"userData\": \"相册\",\n",
    "  \"recognitionModel\": \"recognition_03\"\n",
    "}\n",
    "# 发送请求\n",
    "r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data)\n",
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.添加人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add_face_url =\"https://api-wal.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 人脸相片地址\n",
    "img_url =\"https://i01piccdn.sogoucdn.com/0430aa64bf6411b7\"\n",
    "# 人脸数据\n",
    "params_add_face={\n",
    "    \"userData\":\"guy\"\n",
    "}\n",
    "# 发送请求\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '273630cb-945f-440a-b9dd-aea31606dd89'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json() # 返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.封装成函数方便添加图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-wal.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code #返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://i04piccdn.sogoucdn.com/853a4ceac6a5e205\",\"黄种人\")\n",
    "AddFace(\"https://i01piccdn.sogoucdn.com/cb57e4cd11c5edc1\",\"白人\")\n",
    "AddFace(\"https://i01piccdn.sogoucdn.com/d818194aecb75402\",\"黑人\")\n",
    "AddFace(\"https://i02piccdn.sogoucdn.com/17613aa4d3bd2184\",\"老人\")\n",
    "AddFace(\"https://i04piccdn.sogoucdn.com/b4fcc11756401702\",\"戴帽子的男人\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.检测人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '0c2c4aa5-bbd3-4240-b0e7-6620d07084c4',\n",
       "  'faceRectangle': {'top': 43, 'left': 110, 'width': 59, 'height': 59},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': -7.9, 'roll': 2.8, 'yaw': -4.0},\n",
       "   'gender': 'male',\n",
       "   'age': 49.0,\n",
       "   'facialHair': {'moustache': 0.6, 'beard': 0.6, 'sideburns': 0.6},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.03,\n",
       "    'contempt': 0.009,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.921,\n",
       "    'sadness': 0.041,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.09},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.64},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.13},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [{'type': 'headwear', 'confidence': 1.0}],\n",
       "   'occlusion': {'foreheadOccluded': True,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.0, 'invisible': True, 'hairColor': []}}}]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_api_url = 'https://api-wal.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "image_url = 'https://i04piccdn.sogoucdn.com/853a4ceac6a5e205'\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择模型\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数，请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "response = requests.post(face_api_url, params=params,headers=headers, json={\"url\": image_url})\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '273630cb-945f-440a-b9dd-aea31606dd89',\n",
       "   'userData': 'guy'},\n",
       "  {'persistedFaceId': '46ae1d42-b6ea-4ae7-9830-29d98daf4e8b',\n",
       "   'userData': '黄种人'},\n",
       "  {'persistedFaceId': '8a520c95-cf6e-4821-b306-1079e47d950a',\n",
       "   'userData': '白人'},\n",
       "  {'persistedFaceId': '43b9fca6-6314-4a88-b22e-c304a95b2411',\n",
       "   'userData': '黑人'},\n",
       "  {'persistedFaceId': 'd670e338-dc6b-4b32-b504-858dc882397a',\n",
       "   'userData': '老人'},\n",
       "  {'persistedFaceId': '95cfd5c9-0a3d-48df-8299-d310f0119d70',\n",
       "   'userData': '戴帽子的男人'},\n",
       "  {'persistedFaceId': 'e7da6c5a-e1de-43cd-a487-86f193253d25',\n",
       "   'userData': '黄种人'},\n",
       "  {'persistedFaceId': '6ac5d7b5-6702-4a4d-ac77-5837374c74b3',\n",
       "   'userData': '白人'},\n",
       "  {'persistedFaceId': 'b71c1230-dc6d-4ff1-8613-c3dd620177c3',\n",
       "   'userData': '黑人'},\n",
       "  {'persistedFaceId': '03cb4bf0-1176-49f4-9a1e-40bad2815556',\n",
       "   'userData': '老人'},\n",
       "  {'persistedFaceId': '5de02fb1-9c1a-460e-9d08-12d918649605',\n",
       "   'userData': '戴帽子的男人'}],\n",
       " 'faceListId': 'list_004',\n",
       " 'name': 'sample_list',\n",
       " 'userData': '相册'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_facelist_url = \"https://api-wal.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用Pandas简化数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>273630cb-945f-440a-b9dd-aea31606dd89</td>\n",
       "      <td>guy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46ae1d42-b6ea-4ae7-9830-29d98daf4e8b</td>\n",
       "      <td>黄种人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8a520c95-cf6e-4821-b306-1079e47d950a</td>\n",
       "      <td>白人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>43b9fca6-6314-4a88-b22e-c304a95b2411</td>\n",
       "      <td>黑人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d670e338-dc6b-4b32-b504-858dc882397a</td>\n",
       "      <td>老人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>95cfd5c9-0a3d-48df-8299-d310f0119d70</td>\n",
       "      <td>戴帽子的男人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>e7da6c5a-e1de-43cd-a487-86f193253d25</td>\n",
       "      <td>黄种人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6ac5d7b5-6702-4a4d-ac77-5837374c74b3</td>\n",
       "      <td>白人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>b71c1230-dc6d-4ff1-8613-c3dd620177c3</td>\n",
       "      <td>黑人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>03cb4bf0-1176-49f4-9a1e-40bad2815556</td>\n",
       "      <td>老人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5de02fb1-9c1a-460e-9d08-12d918649605</td>\n",
       "      <td>戴帽子的男人</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   273630cb-945f-440a-b9dd-aea31606dd89      guy\n",
       "1   46ae1d42-b6ea-4ae7-9830-29d98daf4e8b      黄种人\n",
       "2   8a520c95-cf6e-4821-b306-1079e47d950a       白人\n",
       "3   43b9fca6-6314-4a88-b22e-c304a95b2411       黑人\n",
       "4   d670e338-dc6b-4b32-b504-858dc882397a       老人\n",
       "5   95cfd5c9-0a3d-48df-8299-d310f0119d70   戴帽子的男人\n",
       "6   e7da6c5a-e1de-43cd-a487-86f193253d25      黄种人\n",
       "7   6ac5d7b5-6702-4a4d-ac77-5837374c74b3       白人\n",
       "8   b71c1230-dc6d-4ff1-8613-c3dd620177c3       黑人\n",
       "9   03cb4bf0-1176-49f4-9a1e-40bad2815556       老人\n",
       "10  5de02fb1-9c1a-460e-9d08-12d918649605   戴帽子的男人"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# 返回facelist数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.删除人脸/人脸列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "faceListId = \"list_004\" # 需要删除的人脸列表\n",
    "# 删除列表内人脸\n",
    "delete_face_url = \"https://api-wal.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces/{}\"\n",
    "assert subscription_key\n",
    "# 获取上面的persistedFaceId\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"] \n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除人脸列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "delete_facelist_url = \"https://api-wal.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "r_delete_facelist = requests.delete(delete_facelist_url.format(faceListId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
